import subprocess
import os

def run_script(script_path):
    if not os.path.exists(script_path):
        print(f"错误：脚本 {script_path} 不存在，请检查路径。")
        return
    try:
        print(f"正在运行脚本: {script_path}")
        # 关键修改：显式指定 UTF-8 编码，并设置 errors='replace' 避免解码失败
        result = subprocess.run(
            ['python', script_path],
            check=True,
            text=True,          # 返回字符串而非字节
            encoding='utf-8',   # 强制使用 UTF-8 解码输出
            errors='replace',   # 替换非法字符为 �
            capture_output=True
        )
        print(f"脚本 {script_path} 运行成功。")
        print("输出信息:")
        print(result.stdout)
    except subprocess.CalledProcessError as e:
        print(f"脚本 {script_path} 运行失败。")
        print("错误信息:")
        print(e.stderr)  # 此处 stderr 也会自动用 UTF-8 解码

if __name__ == "__main__":
    # 定义脚本文件路径（假设依赖文件已检查）
    processing_data_script = 'data/processing_data.py'
    data_process_script = 'data/Data_process.py'
    data_analyse_script = 'data_analyse.py'
    dataprocess_script = 'dataprocess.py'
    predict_script = 'predict.py'

    # 按顺序运行脚本
    run_script(processing_data_script)
    run_script(data_process_script)
    run_script(data_analyse_script)
    run_script(dataprocess_script)
    run_script(predict_script)

    print("所有脚本已按顺序运行完毕。")